package com.example.demoreptile.article;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


/**
 * Java 爬虫，爬取txt小说
 *
 * @author zhangjunfa
 * @date 2023/8/9 8:50
 */
@Slf4j
public class TestOne {

    public static void main(String[] args) throws IOException {
        IChapterInterImpl impl = new IChapterInterImpl("http://www.bqwu.com");
        // List<Chapter> chapterList = impl.getChapter("https://www.biququ.info/html/9010/");
        List<Chapter> chapterList = impl.getChapter("http://www.bqwu.com/biquge/40_40692/");
        StringBuilder sb = new StringBuilder();
        for (Chapter chapter : chapterList) {
            sb.append(chapter.getTitle()).append("\n\n");
            String content = impl.getContent(chapter.getUrl());
            sb.append(content);
        }
        fileWrite("D:/data/test/", sb.toString(), "qzfs");
    }


    /**
     * 持久化字符串
     * @param path 路径
     * @param content 内容
     * @param txtName 名称
     */
    private static void fileWrite(String path, String content, String txtName) {
        FileOutputStream writer = null;
        OutputStreamWriter out = null;
        try {
            String filePath = path + File.separator + txtName + ".txt";
            File writeFile = new File(filePath);
            File parentFile = writeFile.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            if (!writeFile.exists()) {
                writeFile.createNewFile();
            }
            writer = new FileOutputStream(writeFile, true);
            out = new OutputStreamWriter(writer, StandardCharsets.UTF_8);
            out.write(content + "\r\n");
            log.info("Done.............................{}", LocalDateTime.now());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}